package com.thwcompany.mpdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwcompany.mpdemo.domain.User;
import com.thwcompany.mpdemo.mapper.UserMapper;
import com.thwcompany.mpdemo.param.UserParam;
import com.thwcompany.mpdemo.service.UserService;
import com.thwcompany.mpdemo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-02-24 15:47:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public Page<User> getList(UserParam userParam){
        Page<User> page = new Page<User>();
        page.setCurrent(userParam.getCurrentPage());
        page.setSize(userParam.getSizePage());

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id",0);
        queryWrapper.lt("id",5);

        /**
        //Page<User> page1 = userMapper.getList(page);
        Page<User> page1 = userMapper.getList1(page,queryWrapper);
        //Page<User> page1 = userMapper.getList2(page,3L);
        */

        //--------①
        //Page<User> page1 = userMapper.selectPage(page, null);//结果等同userMapper.getList(page)
        //--------②
        //Page<User> page1 = userMapper.selectPage(page, queryWrapper);//结果等同userMapper.getList1(page,queryWrapper)
        Page<User> page1 = userMapper.selectPage(page, queryWrapper.lambda().between(User::getId, 1, 4));//[1,4]闭区间
        //--------③
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        //Page<User> page1 = userMapper.selectPage(page, queryWrapper1.lambda().eq(User::getId,3));//结果等同userMapper.getList2(page,id)
        //Page<User> page1 =  userMapper.selectPage(page, Wrappers.<User>query().lambda().eq(User::getId,2));//结果同上一行

        return page1;
    }

    // 测试mp的多表关联查询（单表复杂的sql或联表查询，就不能使用mp自带的API了，而要手写sql了）
    @Override
    public Page<UserVo> getUserByIdAndCity(UserParam userParam) {
        Page<User> page = new Page<User>();
        page.setCurrent(userParam.getCurrentPage());
        page.setSize(userParam.getSizePage());

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("a.id",userParam.getId());
        queryWrapper.like("b.city",userParam.getCity());
        queryWrapper.orderByDesc("a.id");
        Page<UserVo> page1 = userMapper.getUserByIdAndCity(page,queryWrapper);
        return page1;
    }

}




